home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / RM_ALL.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  4KB  |  161 lines

  1. /*
  2. **  Remove all files and (optionally) subdirectories
  3. **
  4. **  public domain demo by Bob Stout
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <io.h>
  11. #include <dos.h>
  12. #include <ctype.h>
  13.  
  14. #define LAST_CHAR(str) (str[strlen(str) - 1])
  15. #define MAX_PATH 80
  16.  
  17. #ifdef __TURBOC__
  18.  #include <dir.h>
  19.  #define find_1st(n,a,b) (findfirst((n),(b),(a)))
  20.  #define find_nxt(b) (findnext(b))
  21.  #define find_t ffblk
  22.  #define name ff_name
  23.  #define attrib ff_attrib
  24.  #define _A_SUBDIR FA_DIREC
  25. #else
  26.  #include <direct.h>
  27.  #define find_1st(n,a,b) (_dos_findfirst((n),(a),(b)))
  28.  #define find_nxt(b) (_dos_findnext(b))
  29. #endif
  30.  
  31. /* Select one of the following - remove() is ANSI       */
  32.  
  33. #define rmfunc remove
  34. /* #define rmfunc unlink */
  35.  
  36. #define show(s) fputs((s), stderr)
  37.  
  38. typedef enum {ERROR = -1, SUCCESS, FALSE = 0, TRUE} LOGICAL;
  39. LOGICAL recurse = FALSE, gobble = FALSE;
  40.  
  41. char *mask = "*.*";
  42.  
  43. /*
  44. **  Clean all files from a directory
  45. */
  46.  
  47. void clean_dir(char *path)
  48. {
  49.       char rmpath[MAX_PATH], *rmfile;
  50.       struct find_t fbuf;
  51.  
  52.       strcpy(rmpath, path);
  53.       if ('\\' != LAST_CHAR(rmpath))
  54.             strcat(rmpath, "\\");
  55.       rmfile = &rmpath[strlen(rmpath)];
  56.       strcpy(rmfile, mask);
  57.       if (0 == find_1st(rmpath, 0, &fbuf)) do
  58.       {
  59.             strcpy(rmfile, fbuf.name);
  60.             rmfunc(rmpath);
  61.             printf("deleting %s\n", rmpath);
  62.       } while (0 == find_nxt(&fbuf));
  63. }
  64.  
  65. /*
  66. **  Process directories
  67. */
  68.  
  69. void do_dir(char *path)
  70. {
  71.       char search[MAX_PATH], new[MAX_PATH];
  72.       struct find_t ff;
  73.  
  74.       strcpy(search, path);
  75.       if ('\\' != LAST_CHAR(search))
  76.             strcat(search, "\\");
  77.       strcat(search, "*.*");
  78.       if (SUCCESS == find_1st(search, 0xff, &ff)) do
  79.       {
  80.             if (ff.attrib & _A_SUBDIR && '.' != *ff.name)
  81.             {
  82.                   strcpy(new, path);
  83.                   if ('\\' != LAST_CHAR(new))
  84.                         strcat(new, "\\");
  85.                   strcat(new, ff.name);
  86.                   do_dir(new);
  87.             }
  88.       } while (SUCCESS == find_nxt(&ff));
  89.       clean_dir(path);
  90.       if (gobble)
  91.             rmdir(path);
  92. }
  93.  
  94. /*
  95. **  Tell 'em they messed up
  96. */
  97.  
  98. void usage(LOGICAL errstat)
  99. {
  100.       if (errstat)
  101.             fputc('\a', stderr);
  102.       show("Usage: RM_ALL directory [...directory] [-eFNAME.EXT] [-rg?]\n");
  103.       show("switches: -eFNAME.EXT  Remove only files matching mask "
  104.             "(default is \"-e*.*\")\n");
  105.       show("          -r           Recurse subdirectories\n");
  106.       show("          -g           Gobble (delete) empty subdirectories\n");
  107.       show("          -?           Display help (this message)\n");
  108.       exit(errstat);
  109. }
  110.  
  111. /*
  112. **  RM_ALL - Deletes all files and (optionally) subdirectories
  113. */
  114.  
  115. int main(int argc, char *argv[])
  116. {
  117.       char rmpath[MAX_PATH], *rmfile;
  118.       int i;
  119.       LOGICAL found_dir = FALSE;
  120.       void (*clean_func)(char *) = clean_dir;
  121.  
  122.       for (i = 1; i < argc; ++i)          /* Check for switches         */
  123.       {
  124.             if (NULL == strchr("-/", *argv[i]))
  125.                   continue;               /* Assume it's a filename     */
  126.             switch (toupper(argv[i][1]))
  127.             {
  128.             case 'R':
  129.                   clean_func = do_dir;
  130.                   break;
  131.  
  132.             case 'G':
  133.                   gobble = TRUE;
  134.                   break;
  135.  
  136.             case '?':
  137.                   usage(FALSE);
  138.                   break;
  139.  
  140.             case 'E':
  141.                   if (0 == strlen(&argv[i][2]))
  142.                         usage(ERROR);
  143.                   else  mask = strupr(&argv[i][2]);
  144.                   break;
  145.  
  146.             default:
  147.                   usage(ERROR);
  148.             }
  149.       }
  150.       for (i = 1; i < argc; ++i)          /* Scan filenames             */
  151.       {
  152.             if (strchr("/-", *argv[i]))
  153.                   continue;
  154.             found_dir = TRUE;
  155.             clean_func(argv[i]);
  156.       }
  157.       if (!found_dir)
  158.             usage(TRUE);
  159.       else  return 0;
  160. }
  161.